#coding=utf-8
#import libs
import sys
import mainUI_cmd
import mainUI_sty
import Fun
import os
import tkinter
from   tkinter import *
import tkinter.ttk
import tkinter.font
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg,NavigationToolbar2Tk #NavigationToolbar2TkAgg
from tkinter import messagebox
#Add your Varial Here: (Keep This Line of comments)
#Define UI Class
class  mainUI:
    def __init__(self,root,isTKroot = True):
        uiName = self.__class__.__name__
        Fun.Register(uiName,'UIClass',self)
        self.root = root
        Fun.Register(uiName,'root',self.root)
        style = mainUI_sty.SetupStyle()
        if isTKroot == True:
            self.root.title("船用光纤电流测量装置")
            Fun.CenterDlg(uiName,self.root,1263,691)
            self.root['background'] = '#efefef'
        Form_1= tkinter.Canvas(self.root,width = 10,height = 4)
        Form_1.place(x = 0,y = 0,width = 1263,height = 691)
        Form_1.configure(bg = "#efefef")
        Form_1.configure(highlightthickness = 0)
        Fun.Register(uiName,'Form_1',Form_1)
        Group_1_Variable = Fun.AddTKVariable(uiName,'Group_1')
        Group_1_Variable.set(1)
        #Create the elements of root
        Label_4 = tkinter.Label(Form_1,text="当前采集时间",width = 10,height = 4)
        Fun.Register(uiName,'Label_4',Label_4,'Label_current_time')
        Fun.SetControlPlace(uiName,'Label_4',17,27,149,37)
        Label_4.configure(relief = "flat")
        Label_4_Ft=tkinter.font.Font(family='宋体', size=18,weight='normal',slant='roman',underline=0,overstrike=0)
        Label_4.configure(font = Label_4_Ft)
        SpinBox_9_Variable = Fun.AddTKVariable(uiName,'SpinBox_9','')
        SpinBox_9 = tkinter.Spinbox(Form_1,from_ = 0.0,to = 10.0,increment = 1.0,textvariable = SpinBox_9_Variable)
        Fun.Register(uiName,'SpinBox_9',SpinBox_9)
        Fun.SetControlPlace(uiName,'SpinBox_9',438,30,93,30)
        SpinBox_9.configure(relief = "groove")
        Label_10 = tkinter.Label(Form_1,text="设定采集时间",width = 10,height = 4)
        Fun.Register(uiName,'Label_10',Label_10)
        Fun.SetControlPlace(uiName,'Label_10',288,31,145,29)
        Label_10.configure(relief = "flat")
        Label_10_Ft=tkinter.font.Font(family='宋体', size=18,weight='normal',slant='roman',underline=0,overstrike=0)
        Label_10.configure(font = Label_10_Ft)
        Label_11 = tkinter.Label(Form_1,text="串行端口",width = 10,height = 4)
        Fun.Register(uiName,'Label_11',Label_11)
        Fun.SetControlPlace(uiName,'Label_11',563,27,103,33)
        Label_11.configure(relief = "flat")
        Label_11_Ft=tkinter.font.Font(family='宋体', size=18,weight='normal',slant='roman',underline=0,overstrike=0)
        Label_11.configure(font = Label_11_Ft)
        ComboBox_12_Variable = Fun.AddTKVariable(uiName,'ComboBox_12')
        ComboBox_12 = tkinter.ttk.Combobox(Form_1,textvariable=ComboBox_12_Variable, state="readonly")
        Fun.Register(uiName,'ComboBox_12',ComboBox_12)
        Fun.SetControlPlace(uiName,'ComboBox_12',670,26,92,34)
        ComboBox_12.configure(state = "readonly")
        ComboBox_12["values"]=['COM1','COM2','COM3']
        ComboBox_12.current(0)
        Label_13 = tkinter.Label(Form_1,text="波特率",width = 10,height = 4)
        Fun.Register(uiName,'Label_13',Label_13)
        Fun.SetControlPlace(uiName,'Label_13',803,31,82,29)
        Label_13.configure(relief = "flat")
        Label_13_Ft=tkinter.font.Font(family='宋体', size=18,weight='normal',slant='roman',underline=0,overstrike=0)
        Label_13.configure(font = Label_13_Ft)
        ComboBox_14_Variable = Fun.AddTKVariable(uiName,'ComboBox_14')
        ComboBox_14 = tkinter.ttk.Combobox(Form_1,textvariable=ComboBox_14_Variable, state="readonly")
        Fun.Register(uiName,'ComboBox_14',ComboBox_14)
        Fun.SetControlPlace(uiName,'ComboBox_14',894,27,77,33)
        ComboBox_14.configure(state = "readonly")
        ComboBox_14["values"]=['57600','2M/s']
        ComboBox_14.current(0)
        Label_15 = tkinter.Label(Form_1,text="系统温度",width = 10,height = 4)
        Fun.Register(uiName,'Label_15',Label_15)
        Fun.SetControlPlace(uiName,'Label_15',996,80,104,32)
        Label_15.configure(relief = "flat")
        Label_15_Ft=tkinter.font.Font(family='宋体', size=18,weight='normal',slant='roman',underline=0,overstrike=0)
        Label_15.configure(font = Label_15_Ft)
        Entry_8_Variable = Fun.AddTKVariable(uiName,'Entry_8','')
        Entry_8 = tkinter.Entry(Form_1,textvariable=Entry_8_Variable)
        Fun.Register(uiName,'Entry_8',Entry_8,'Entry_current_time')
        Fun.SetControlPlace(uiName,'Entry_8',166,30,100,30)
        Entry_8.configure(relief = "sunken")
        Entry_16_Variable = Fun.AddTKVariable(uiName,'Entry_16','')
        Entry_16 = tkinter.Entry(Form_1,textvariable=Entry_16_Variable)
        Fun.Register(uiName,'Entry_16',Entry_16)
        Fun.SetControlPlace(uiName,'Entry_16',1113,83,104,29)
        Entry_16.configure(relief = "sunken")
        Label_17 = tkinter.Label(Form_1,text="原始输出",width = 10,height = 4)
        Fun.Register(uiName,'Label_17',Label_17)
        Fun.SetControlPlace(uiName,'Label_17',996,124,104,25)
        Label_17.configure(relief = "flat")
        Label_17_Ft=tkinter.font.Font(family='宋体', size=18,weight='normal',slant='roman',underline=0,overstrike=0)
        Label_17.configure(font = Label_17_Ft)
        Entry_18_Variable = Fun.AddTKVariable(uiName,'Entry_18','')
        Entry_18 = tkinter.Entry(Form_1,textvariable=Entry_18_Variable)
        Fun.Register(uiName,'Entry_18',Entry_18)
        Fun.SetControlPlace(uiName,'Entry_18',1113,121,104,28)
        Entry_18.configure(relief = "sunken")
        Label_19 = tkinter.Label(Form_1,text="分频显示",width = 10,height = 4)
        Fun.Register(uiName,'Label_19',Label_19)
        Fun.SetControlPlace(uiName,'Label_19',996,165,104,26)
        Label_19.configure(relief = "flat")
        Label_19_Ft=tkinter.font.Font(family='宋体', size=18,weight='normal',slant='roman',underline=0,overstrike=0)
        Label_19.configure(font = Label_19_Ft)
        Entry_20_Variable = Fun.AddTKVariable(uiName,'Entry_20','')
        Entry_20 = tkinter.Entry(Form_1,textvariable=Entry_20_Variable)
        Fun.Register(uiName,'Entry_20',Entry_20)
        Fun.SetControlPlace(uiName,'Entry_20',1113,162,104,29)
        Entry_20.configure(relief = "sunken")
        Label_21 = tkinter.Label(Form_1,text="总状态字",width = 10,height = 4)
        Fun.Register(uiName,'Label_21',Label_21)
        Fun.SetControlPlace(uiName,'Label_21',998,203,102,24)
        Label_21.configure(relief = "flat")
        Label_21_Ft=tkinter.font.Font(family='宋体', size=18,weight='normal',slant='roman',underline=0,overstrike=0)
        Label_21.configure(font = Label_21_Ft)
        Entry_22_Variable = Fun.AddTKVariable(uiName,'Entry_22','')
        Entry_22 = tkinter.Entry(Form_1,textvariable=Entry_22_Variable)
        Fun.Register(uiName,'Entry_22',Entry_22)
        Fun.SetControlPlace(uiName,'Entry_22',1113,200,104,27)
        Entry_22.configure(relief = "sunken")
        Label_23 = tkinter.Label(Form_1,text="半波电压",width = 10,height = 4)
        Fun.Register(uiName,'Label_23',Label_23)
        Fun.SetControlPlace(uiName,'Label_23',998,238,102,28)
        Label_23.configure(relief = "flat")
        Label_23_Ft=tkinter.font.Font(family='宋体', size=18,weight='normal',slant='roman',underline=0,overstrike=0)
        Label_23.configure(font = Label_23_Ft)
        Label_24 = tkinter.Label(Form_1,text="光功率值",width = 10,height = 4)
        Fun.Register(uiName,'Label_24',Label_24)
        Fun.SetControlPlace(uiName,'Label_24',998,275,102,26)
        Label_24.configure(relief = "flat")
        Label_24_Ft=tkinter.font.Font(family='宋体', size=18,weight='normal',slant='roman',underline=0,overstrike=0)
        Label_24.configure(font = Label_24_Ft)
        Label_25 = tkinter.Label(Form_1,text="噪声",width = 10,height = 4)
        Fun.Register(uiName,'Label_25',Label_25)
        Fun.SetControlPlace(uiName,'Label_25',998,309,102,28)
        Label_25.configure(relief = "flat")
        Label_25_Ft=tkinter.font.Font(family='宋体', size=18,weight='normal',slant='roman',underline=0,overstrike=0)
        Label_25.configure(font = Label_25_Ft)
        Label_27 = tkinter.Label(Form_1,text="电流",width = 10,height = 4)
        Fun.Register(uiName,'Label_27',Label_27)
        Fun.SetControlPlace(uiName,'Label_27',998,381,102,30)
        Label_27.configure(relief = "flat")
        Label_27_Ft=tkinter.font.Font(family='宋体', size=18,weight='normal',slant='roman',underline=0,overstrike=0)
        Label_27.configure(font = Label_27_Ft)
        Label_28 = tkinter.Label(Form_1,text="比差",width = 10,height = 4)
        Fun.Register(uiName,'Label_28',Label_28)
        Fun.SetControlPlace(uiName,'Label_28',1000,419,100,27)
        Label_28.configure(relief = "flat")
        Label_28_Ft=tkinter.font.Font(family='宋体', size=18,weight='normal',slant='roman',underline=0,overstrike=0)
        Label_28.configure(font = Label_28_Ft)
        Label_29 = tkinter.Label(Form_1,text="标准电流",width = 10,height = 4)
        Fun.Register(uiName,'Label_29',Label_29)
        Fun.SetControlPlace(uiName,'Label_29',1000,454,100,26)
        Label_29.configure(relief = "flat")
        Label_29_Ft=tkinter.font.Font(family='宋体', size=18,weight='normal',slant='roman',underline=0,overstrike=0)
        Label_29.configure(font = Label_29_Ft)
        Label_30 = tkinter.Label(Form_1,text="开启计算",width = 10,height = 4)
        Fun.Register(uiName,'Label_30',Label_30)
        Fun.SetControlPlace(uiName,'Label_30',1000,489,100,31)
        Label_30.configure(relief = "flat")
        Label_30_Ft=tkinter.font.Font(family='宋体', size=18,weight='normal',slant='roman',underline=0,overstrike=0)
        Label_30.configure(font = Label_30_Ft)
        Label_26 = tkinter.Label(Form_1,text="告警累计",width = 10,height = 4)
        Fun.Register(uiName,'Label_26',Label_26)
        Fun.SetControlPlace(uiName,'Label_26',1000,337,104,29)
        Label_26.configure(relief = "flat")
        Label_26_Ft=tkinter.font.Font(family='宋体', size=18,weight='normal',slant='roman',underline=0,overstrike=0)
        Label_26.configure(font = Label_26_Ft)
        Button_31 = tkinter.Button(Form_1,text="开始录波",width = 10,height = 4)
        Fun.Register(uiName,'Button_31',Button_31)
        Fun.SetControlPlace(uiName,'Button_31',60,610,167,60)
        Button_31_Ft=tkinter.font.Font(family='宋体', size=18,weight='normal',slant='roman',underline=0,overstrike=0)
        Button_31.configure(font = Button_31_Ft)
        Button_32 = tkinter.Button(Form_1,text="结束录波",width = 10,height = 4)
        Fun.Register(uiName,'Button_32',Button_32)
        Fun.SetControlPlace(uiName,'Button_32',305,609,156,61)
        Button_32_Ft=tkinter.font.Font(family='宋体', size=18,weight='normal',slant='roman',underline=0,overstrike=0)
        Button_32.configure(font = Button_32_Ft)
        Button_33 = tkinter.Button(Form_1,text="保存数据",width = 10,height = 4)
        Fun.Register(uiName,'Button_33',Button_33)
        Fun.SetControlPlace(uiName,'Button_33',538,609,153,61)
        Button_33_Ft=tkinter.font.Font(family='宋体', size=18,weight='normal',slant='roman',underline=0,overstrike=0)
        Button_33.configure(font = Button_33_Ft)
        Button_34 = tkinter.Button(Form_1, text="调取历史数据", width = 10, height = 4)
        Fun.Register(uiName,'Button_34',Button_34)
        Fun.SetControlPlace(uiName,'Button_34',772,607,154,63)
        Button_34_Ft=tkinter.font.Font(family='宋体', size=18,weight='normal',slant='roman',underline=0,overstrike=0)
        Button_34.configure(font = Button_34_Ft)
        Label_35 = tkinter.Label(Form_1,text="预警告示",width = 10,height = 4)
        Fun.Register(uiName,'Label_35',Label_35)
        Fun.SetControlPlace(uiName,'Label_35',1000,525,102,28)
        Label_35.configure(relief = "flat")
        Label_35_Ft=tkinter.font.Font(family='宋体', size=18,weight='normal',slant='roman',underline=0,overstrike=0)
        Label_35.configure(font = Label_35_Ft)
        Label_36 = tkinter.Label(Form_1,text="告警指示",width = 10,height = 4)
        Fun.Register(uiName,'Label_36',Label_36)
        Fun.SetControlPlace(uiName,'Label_36',1002,558,100,32)
        Label_36.configure(relief = "flat")
        Label_36_Ft=tkinter.font.Font(family='宋体', size=18,weight='normal',slant='roman',underline=0,overstrike=0)
        Label_36.configure(font = Label_36_Ft)
        Label_37 = tkinter.Label(Form_1,text="半波过压",width = 10,height = 4)
        Fun.Register(uiName,'Label_37',Label_37)
        Fun.SetControlPlace(uiName,'Label_37',1002,595,100,31)
        Label_37.configure(relief = "flat")
        Label_37_Ft=tkinter.font.Font(family='宋体', size=18,weight='normal',slant='roman',underline=0,overstrike=0)
        Label_37.configure(font = Label_37_Ft)
        Label_38 = tkinter.Label(Form_1,text="参数待写",width = 10,height = 4)
        Fun.Register(uiName,'Label_38',Label_38)
        Fun.SetControlPlace(uiName,'Label_38',1002,626,100,33)
        Label_38.configure(relief = "flat")
        Label_38_Ft=tkinter.font.Font(family='宋体', size=18,weight='normal',slant='roman',underline=0,overstrike=0)
        Label_38.configure(font = Label_38_Ft)
        Entry_39_Variable = Fun.AddTKVariable(uiName,'Entry_39','')
        Entry_39 = tkinter.Entry(Form_1,textvariable=Entry_39_Variable)
        Fun.Register(uiName,'Entry_39',Entry_39)
        Fun.SetControlPlace(uiName,'Entry_39',1112,238,105,28)
        Entry_39.configure(relief = "sunken")
        Entry_40_Variable = Fun.AddTKVariable(uiName,'Entry_40','')
        Entry_40 = tkinter.Entry(Form_1,textvariable=Entry_40_Variable)
        Fun.Register(uiName,'Entry_40',Entry_40)
        Fun.SetControlPlace(uiName,'Entry_40',1112,275,105,26)
        Entry_40.configure(relief = "sunken")
        Entry_41_Variable = Fun.AddTKVariable(uiName,'Entry_41','')
        Entry_41 = tkinter.Entry(Form_1,textvariable=Entry_41_Variable)
        Fun.Register(uiName,'Entry_41',Entry_41)
        Fun.SetControlPlace(uiName,'Entry_41',1112,308,105,29)
        Entry_41.configure(relief = "sunken")
        Entry_42_Variable = Fun.AddTKVariable(uiName,'Entry_42','')
        Entry_42 = tkinter.Entry(Form_1,textvariable=Entry_42_Variable)
        Fun.Register(uiName,'Entry_42',Entry_42)
        Fun.SetControlPlace(uiName,'Entry_42',1112,344,105,29)
        Entry_42.configure(relief = "sunken")
        Entry_43_Variable = Fun.AddTKVariable(uiName,'Entry_43','')
        Entry_43 = tkinter.Entry(Form_1,textvariable=Entry_43_Variable)
        Fun.Register(uiName,'Entry_43',Entry_43)
        Fun.SetControlPlace(uiName,'Entry_43',1112,381,105,30)
        Entry_43.configure(relief = "sunken")
        Entry_44_Variable = Fun.AddTKVariable(uiName,'Entry_44','')
        Entry_44 = tkinter.Entry(Form_1,textvariable=Entry_44_Variable)
        Fun.Register(uiName,'Entry_44',Entry_44)
        Fun.SetControlPlace(uiName,'Entry_44',1111,419,106,27)
        Entry_44.configure(relief = "sunken")
        ComboBox_46_Variable = Fun.AddTKVariable(uiName,'ComboBox_46')
        ComboBox_46 = tkinter.ttk.Combobox(Form_1,textvariable=ComboBox_46_Variable, state="readonly")
        Fun.Register(uiName,'ComboBox_46',ComboBox_46)
        Fun.SetControlPlace(uiName,'ComboBox_46',1113,455,104,25)
        ComboBox_46.configure(state = "readonly")
        Label_47 = tkinter.Label(Form_1,text="",width = 10,height = 4)
        Fun.Register(uiName,'Label_47',Label_47)
        Fun.SetControlPlace(uiName,'Label_47',1115,523,102,30)
        Label_47.configure(bg = "#00ff80")
        Label_47.configure(relief = "flat")
        Label_48 = tkinter.Label(Form_1,text="",width = 10,height = 4)
        Fun.Register(uiName,'Label_48',Label_48)
        Fun.SetControlPlace(uiName,'Label_48',1115,561,102,29)
        Label_48.configure(bg = "#00ff80")
        Label_48.configure(relief = "flat")
        Label_49 = tkinter.Label(Form_1,text="",width = 10,height = 4)
        Fun.Register(uiName,'Label_49',Label_49)
        Fun.SetControlPlace(uiName,'Label_49',1115,595,102,26)
        Label_49.configure(bg = "#00ff80")
        Label_49.configure(relief = "flat")
        Label_50 = tkinter.Label(Form_1,text="",width = 10,height = 4)
        Fun.Register(uiName,'Label_50',Label_50)
        Fun.SetControlPlace(uiName,'Label_50',1115,630,102,29)
        Label_50.configure(bg = "#00ff80")
        Label_50.configure(relief = "flat")
        RadioButton_51 = tkinter.Radiobutton(Form_1,variable=Group_1_Variable,value=1,text="RadioButton",anchor=tkinter.W)
        Fun.Register(uiName,'RadioButton_51',RadioButton_51,None,'Group_1')
        Fun.SetControlPlace(uiName,'RadioButton_51',1115,492,100,20)
        Canvas_52 = tkinter.Canvas(Form_1)
        Fun.Register(uiName,'Canvas_52',Canvas_52)
        Fun.SetControlPlace(uiName,'Canvas_52',22,83,945,516)
        Canvas_52.configure(bg = "#ffffff")
        #Inital all element's Data
        Fun.InitElementData(uiName)
        #Add Some Logic Code Here: (Keep This Line of comments)
        #创建主窗体
        self.figure = self.create_matplotlib()
        self.create_form(self.figure)
        Button_34.configure(command=lambda: self.Button_34_click())

    #调取历史数据事件监听
    def Button_34_click(self):
        fdir = tkinter.filedialog.askopenfilename()
        str_list = fdir.split('/')
        if(fdir == ""):
            messagebox.showerror("提示", "选择的文件不能为空！")
        elif(str_list[-1].split('.')[-1]!="xlsx"):
            messagebox.showerror("提示","选择的文件不符合要求，需是xlsx文件！")
        else:
            df = pd.read_excel(fdir)
            plt.plot(df["Time"], df["Voltage"], label='Voltage', linewidth=1, color='r', marker='o',
                     markerfacecolor='blue', markersize=1)
            plt.xlabel("Time")
            plt.ylabel('Voltage')
            plt.title("Power_System_Supervise")
            plt.legend()
            plt.grid()
            plt.show()

    def create_matplotlib(self):
        df = pd.read_excel("Time_Voltage.xlsx")
        f = plt.figure(figsize=(9.5,5),frameon=True)
        fig1 = plt.subplot(1,1,1)
        plt.plot(df["Time"], df["Voltage"], label='Voltage', linewidth=1, color='r', marker='o', markerfacecolor='blue',
                 markersize=1)
        fig1.set_title("Power_System_Supervise")
        fig1.set_xlabel('Time')
        fig1.set_ylabel("Voltage")
        fig1.grid(which='major', axis='x', color='r', linestyle='-', linewidth=2)  # 设置网格
        fig1.legend()
        return f

    def create_form(self,figure):
        self.Canvas_52 = FigureCanvasTkAgg(figure,self.root)
        self.Canvas_52.draw()
        self.Canvas_52.get_tk_widget().pack(side ='left',pady=85)


#Create the root of Kinter
if  __name__ == '__main__':
    root = tkinter.Tk()
    MyDlg = mainUI(root)
    root.mainloop()
